﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Configuration;

namespace PaulBeck.Timesheet.BOL
{
    public static class Common
    {

        public static string GetTimesheetConnectioninfo()
        {
            string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
            return connectionInfo;
        }

        public static Status GetWeekStatus(int weekId)
        {
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                WeekEntry week = dc.WeekEntries.Single(r => r.WeekEntryId == weekId);
                string weekStatus = week.Status.Trim();
                if (weekStatus == BOL.Status.Missing.ToString())
                {
                    return BOL.Status.Missing;
                }
                else if (weekStatus == BOL.Status.Approved.ToString())
                {
                    return BOL.Status.Approved;
                }
                else if (weekStatus == BOL.Status.Rejected.ToString())
                {
                    return BOL.Status.Rejected;
                }
                else if (weekStatus == BOL.Status.Submitted.ToString())
                {
                    return BOL.Status.Submitted;
                }
                else
                {
                    return BOL.Status.Unsubmitted;
                }
            }
            catch (InvalidOperationException ex)
            {
                // Occurs if no weekId exists.
                return BOL.Status.Unsubmitted;
            }
        }

        public static ListItemCollection GetWeekCommencingDates(Int32 UserId, DateTime FocusWeek, string LoginName)
        {
            if (UserId != 0)
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                // Get the latest weeks for the consultant
                var week = from w in dc.WeekEntries
                           where w.ResourceId == UserId
                           orderby w.WeekStartDate descending
                           select new
                           {
                               w.WeekStartDate,
                               w.Status,
                               w.WeekEntryId
                           };

                ListItemCollection lic = new ListItemCollection();
                DateTime SatComing = DateTime.Now.AddDays((int)(DayOfWeek.Saturday - DateTime.Now.DayOfWeek));
                DateTime[] Saturdays = new DateTime[10];
                for (int countWeeks = 0; countWeeks < 10; countWeeks++)
                {
                    int adjustment = ((countWeeks * 7) * -1);
                    Saturdays[countWeeks] = SatComing.AddDays(adjustment);
                    bool existsInDb = false;
                    foreach (var item in week)
                    {
                        if (item.WeekStartDate.ToShortDateString() == Saturdays[countWeeks].ToShortDateString())
                        {
                            string ui = string.Format("{0} ({1})", item.WeekStartDate.ToShortDateString(), item.Status);
                            lic.Add(new ListItem(ui, item.WeekEntryId.ToString()));
                            existsInDb = true;
                        }
                    }
                    if (!existsInDb)
                    {
                        if ((countWeeks == 0) || (Saturdays[countWeeks].ToShortDateString() == FocusWeek.ToShortDateString()))
                        {
                            WeekEntry weekCreate = new WeekEntry();
                            weekCreate.ResourceId = UserId;
                            weekCreate.WeekStartDate = Convert.ToDateTime(FocusWeek.ToShortDateString());
                            weekCreate.CreatedBy = LoginName;
                            weekCreate.CreatedDate = DateTime.Now;
                            weekCreate.Status = Status.Unsubmitted.ToString();
                            //// Add days of the week
                            for (int countDays = 7; countDays > 0; countDays--)
                            {
                                DayEntry day = new DayEntry();
                                day.DayDate = FocusWeek.AddDays((countDays) * -1);
                                day.CreatedBy = LoginName;
                                day.CreatedDate = DateTime.Now;
                                day.DayNumber = countDays;

                                weekCreate.DayEntries.Add(day);
                            }
                            dc.WeekEntries.InsertOnSubmit(weekCreate);
                            dc.SubmitChanges();
                            string ui = string.Format("{0} ({1})", Saturdays[countWeeks].ToShortDateString(), Status.Unsubmitted);
                            lic.Add(new ListItem(ui, (countWeeks * -1).ToString()));
                        }
                        else
                        {
                            string ui = string.Format("{0} ({1})", Saturdays[countWeeks].ToShortDateString(), Status.Missing);
                            lic.Add(new ListItem(ui, (countWeeks * -1).ToString()));
                        }
                    }
                }
                return lic;
            }
            else
            {
                return null;
            }
        }

        public static bool UpdateWeekEntries(BOL.Status status, int weekId, string LoginName)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            WeekEntry week = dc.WeekEntries.Single(r => r.WeekEntryId == weekId);
            week.Status = status.ToString();
            week.LastUpdatedDate = DateTime.Now;
            week.LastUpdatedBy = LoginName;
            dc.SubmitChanges();
            return true;
        }

    }



    public enum Status
    {
        Unsubmitted,
        Submitted,
        Approved,
        Rejected,
        Missing
    }
}
